package com.example.tellwin.mine.act;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.mine.contract.FeedBackContract;
import com.example.tellwin.mine.presenter.FeedBackPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedBackActivity extends CpBaseActivty implements FeedBackContract.View {

    @BindView(R.id.feedback_et)
    EditText feedbackEt;
    @BindView(R.id.size_tv)
    TextView sizeTv;
    @BindView(R.id.commit_btn)
    TextView commitBtn;

    @Inject
    FeedBackPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void initEvents() {
        feedbackEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                int length = s.toString().length();
                sizeTv.setText(length + "/" + 200);
                if(length>0){
                    commitBtn.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void initDatas() {
        setTitle("意见反馈");
    }

    @OnClick(R.id.commit_btn)
    public void onViewClicked() {
        mPresenter.feedback(feedbackEt.getText().toString());
    }

    @Override
    public void feedbackSuccess() {
        finish();
    }
}
